//
//  SIEmoticonInputView.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/13.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit

private let cellId = "cellId"

class SIEmoticonInputView: UIView {
    
    private var selectedEmoticonCallBack: ((emoticon: SIEmoticon?) -> ())?

    @IBOutlet weak var collectionVIew: UICollectionView!
    
    @IBOutlet weak var toolbar: SIEmoticonToolbar!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    class func inputView(selectedEmoticon: (emoticon: SIEmoticon?) -> ()) -> SIEmoticonInputView {
        
        let nib = UINib(nibName: "SIEmoticonInputView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! SIEmoticonInputView
        
        v.selectedEmoticonCallBack = selectedEmoticon
        
        return v
    }
    
    override func awakeFromNib() {
        
        collectionVIew.isPagingEnabled = true
        collectionVIew.dataSource = self
        collectionVIew.delegate = self
        
        collectionVIew.register(SIEmoticonCell.self, forCellWithReuseIdentifier: cellId)
        
        toolbar.delegate = self
        
        let bundle = SIEmotionManager.shared.bundle
        
        guard let normalImage = UIImage(named: "compose_keyboard_dot_normal", in: bundle, compatibleWith: nil),
            selectedImage = UIImage(named: "compose_keyboard_dot_selected", in: bundle, compatibleWith: nil) else {
                return
        }
        
        pageControl.setValue(normalImage, forKey: "_pageImage")
        pageControl.setValue(selectedImage, forKey: "_currentPageImage")

        
    }

}

extension SIEmoticonInputView: SIEmoticonToolbarDelegate {
    
    func emoticonToolbarDidSelectedItemIndex(toolbar: SIEmoticonToolbar, index: Int) {
        
        let indexPath = IndexPath(item: 0, section: index)
        
        collectionVIew.scrollToItem(at: indexPath, at: .left, animated: true)
        
        toolbar.selectedIndex = index
    }
}

extension SIEmoticonInputView: UICollectionViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        var center = scrollView.center
        center.x += scrollView.contentOffset.x
        
        let paths = collectionVIew.indexPathsForVisibleItems()
        
        var targetIndexPath: IndexPath?
        
        for indexPath in paths {
            
            let cell = collectionVIew.cellForItem(at: indexPath)
            
            if cell?.frame.contains(center) == true {
                targetIndexPath = indexPath
                
                break
            }
        }
        
        guard let target = targetIndexPath else {
            return
        }
        
        toolbar.selectedIndex = target.section
        
        pageControl.numberOfPages = collectionVIew.numberOfItems(inSection: target.section)
        pageControl.currentPage = target.item
        
    }

    
}

extension SIEmoticonInputView: UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return SIEmotionManager.shared.packages.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return SIEmotionManager.shared.packages[section].numberOfPages
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionVIew.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SIEmoticonCell
        
//        cell.label.text = "\(indexPath.section) - \(indexPath.item)"
        cell.emoticons = SIEmotionManager.shared.packages[indexPath.section].emoticon(page: indexPath.item)
        
        cell.delegate = self

        return cell
        
    }
    
}

extension SIEmoticonInputView: SIEmoticonCellDelegate {
    /// 选中的表情回调
    func emoticonCellDidSelectedEmoticon(cell: SIEmoticonCell, em: SIEmoticon?) {
        
        selectedEmoticonCallBack?(emoticon: em)
        
        guard let em = em else {
            return
        }
        
        let indexPath = collectionVIew.indexPathsForVisibleItems()[0]
        
        if indexPath.section == 0 {
            return
        }
        
        SIEmotionManager.shared.recentEmoticon(em: em)
        
        var indexSet = IndexSet()
        
        indexSet.insert(0)
        
        collectionVIew.reloadSections(indexSet)
    }
    
}
